<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\services\setting;

use app\adminapi\validate\setting\SystemDeptValidate;
use app\dao\setting\SystemDeptDao;
use Exception;

class SystemDeptService
{
    private SystemDeptDao $dao;

    public function __construct(SystemDeptDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 部门列表
     * @param array $param ['page' => '', 'pageSize' => '']
     */
    public function listSystemDept(array $param)
    {
        try {
            $where[] = ['delete_flag', '=', 0];
            if (!empty($param['keyword'])) {
                $where[] = ['label', 'like', '%' . $param['keyword'] . '%'];
            }
            $deptList = $this->dao->listSystemDept($where);
            $deptList = make_tree($deptList);

            return success($deptList);
        } catch (Exception $e) {
            return fail($e->getMessage());
        }
    }

    /**
     * 编辑部门
     * @param array $param
     */
    public function editSystemDept(array $param = [
        'id' => '',
        'pid' => '',
        'label' => '',
        'remark' => '',
        'sort' => '',
        'status' => '',
        'deleteFlag' => ''
    ])
    {
        try {

            Validate(SystemDeptValidate::class)->scene('editSystemDept')->check($param);

            // 部门数据
            $updateData = [
                'pid' => $param['pid'],
                'label' => $param['label'],
                'remark' => $param['remark'],
                'sort' => $param['sort'],
                'status' => $param['status'],
                'delete_flag' => $param['deleteFlag']
            ];

            if (!empty($param['deleteFlag'])) {
                $updateData['delete_time'] = now();
            }

            if (!empty($param['id'])) {
                // 更新部门
                $updateData['update_time'] = now();
                $id = $this->dao->update($param['id'], $updateData, 'id')->label;
            } else {
                // 新建部门
                $updateData['create_time'] = now();
                $id = $this->dao->save($updateData)->id;
            }

            return success(['id' => $id]);
        } catch (Exception $e) {
            return fail($e->getMessage());
        }
    }

    /**
     * 批量删除部门
     * @param array $param
     */
    public function batchDelSystemDept(array $param = ['ids' => []])
    {
        try {
            Validate(SystemDeptValidate::class)->scene('batchDelSystemDept')->check($param);

            $where[] = ['id', 'in', $param['ids']];
            $this->dao->update($where, [
                'delete_flag' => 1,
                'delete_time' => now()
            ]);

            return success();
        } catch (Exception $e) {
            return fail($e->getMessage());
        }
    }
}